package com.szzg10.webservice.chapter3;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.jws.HandlerChain;
import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
@HandlerChain(file = "handler-chain-server.xml")
public class RabbitCounter {

	private Map<Integer, Integer> cache = Collections.synchronizedMap(new HashMap<Integer, Integer>());
	
	@WebMethod
	public int countRabbits(int n) throws FibException {
		
		if(n < 0) {
			throw new FibException("Neg. arg. not allowed.", n + " < 0");
		}
		
		if(n < 2) {
			return n;
		}
		
		if(cache.containsKey(n)) {
			return cache.get(n);
		}
		
		if(cache.containsKey(n - 1) && cache.containsKey(n - 2)) {
			
			cache.put(n, cache.get(n - 1) + cache.get(n - 2));
			return cache.get(n);
		}
		
		int fib = 1;
		int prev = 0;
		for(int i = 2; i <= n; i++) {
			
			int temp = fib;
			fib += prev;
			prev = temp;
		}
		
		cache.put(n, fib);
		
		return fib;
	}
}
